﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Chequeo_de_Bolsas_Reembolse
{
    public partial class frmPrincipal : Form
    {

        public frmPrincipal()
        {
            InitializeComponent();
        }

        private void btnComenzar_Click(object sender, EventArgs e)
        {
            frmProceso proceso = new frmProceso();

            if (chkTerminar.Checked)
            {
                //traspaso de datos para configurar el chequeo
                proceso.cargarVariables(txtCodMaterial.Text, txtDescMaterial.Text, txtLotMaterial.Text, txtCantMaterial.Text, "SI");
            }
            else
            {
                //traspaso de datos para configurar el chequeo
                proceso.cargarVariables(txtCodMaterial.Text, txtDescMaterial.Text, txtLotMaterial.Text, txtCantMaterial.Text, "NO");
            }

            proceso.Show();
           
        }

        private void frmPrincipal_Load(object sender, EventArgs e)
        {
            lblCantidad.Visible = false;
            txtCantMaterial.Visible = false;
            cargarData();
        }

        private void cargarData()
        {
            DataSet ds = DatosReembolse.leerDatos();
            try
            {
                if (ds != null)
                {
                    bindingSource1.DataSource = ds.Tables[0];
                    bindingNavigator1.BindingSource = bindingSource1;
                    dgvBaseReembolse.DataSource = bindingSource1;
                    dgvBaseReembolse.Columns[0].HeaderText = "Código Material";
                    dgvBaseReembolse.Columns[0].Width = 130;
                    dgvBaseReembolse.Columns[1].HeaderText = "Descripción";
                    dgvBaseReembolse.Columns[1].Width = 230;
                    dgvBaseReembolse.Columns[2].HeaderText = "Lote";
                    dgvBaseReembolse.Columns[2].Width=100;

                    tslcmbSelFiltro.Text = "Filtrar por...";

                    for (int i = 0; i < dgvBaseReembolse.ColumnCount; i++)
                    {
                        tslcmbSelFiltro.Items.Add(dgvBaseReembolse.Columns[i].HeaderText);
                    }

                    tsbtnBuscar.Visible = false;
                }
                else
                {
                    MessageBox.Show("Error al intentar cargar los datos!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch
            {
                MessageBox.Show("Error al intentar cargar los datos!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dgvBaseReembolse_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (null != dgvBaseReembolse.CurrentRow.Cells["Material"].Value.ToString() && null != dgvBaseReembolse.CurrentRow.Cells["Descripcion"].Value.ToString()
                    && null != dgvBaseReembolse.CurrentRow.Cells["Lote"].Value.ToString())
                {

                    txtCodMaterial.Text = dgvBaseReembolse.CurrentRow.Cells["Material"].Value.ToString();
                    txtDescMaterial.Text = dgvBaseReembolse.CurrentRow.Cells["Descripcion"].Value.ToString();
                    txtLotMaterial.Text = dgvBaseReembolse.CurrentRow.Cells["Lote"].Value.ToString();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("No existen datos para seleccionar!!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            txtLotMaterial.Text = "";
            txtDescMaterial.Text = "";
            txtCodMaterial.Text = "";
            txtCantMaterial.Text = "";
            chkTerminar.Checked = false;
        }

        private void chkTerminar_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTerminar.Checked)
            {
                lblCantidad.Visible = true;
                txtCantMaterial.Visible = true;
            }
            else
            {
                lblCantidad.Visible = false;
                txtCantMaterial.Visible = false;
            }
        }

        private void tstxtFiltro_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(tstxtFiltro.Text.Trim()))
                {
                    tsbtnBuscar.Visible = false;
                    tsbtnBuscar.Enabled = false;
                    this.bindingSource1.RemoveFilter();
                    return;
                }
                if (tslcmbSelFiltro.Text.Equals("Código Material"))
                {
                    tsbtnBuscar.Visible = true;
                    tsbtnBuscar.Enabled = true;
                }

                else if (tslcmbSelFiltro.Text.Equals("Descripción"))
                {
                    tsbtnBuscar.Visible = false;
                    tsbtnBuscar.Enabled = false;
                    this.bindingSource1.Filter = "Descripcion LIKE '%" + this.tstxtFiltro.Text + "%'";
                }

                else if (tslcmbSelFiltro.Text.Equals("Lote"))
                {
                    tsbtnBuscar.Visible = false;
                    tsbtnBuscar.Enabled = false;
                    this.bindingSource1.Filter = "Lote LIKE '%" + this.tstxtFiltro.Text + "%'";
                }
                
            }
            catch (Exception)
            {
                MessageBox.Show("Error al aplicar filtro!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsbtnBuscar_Click(object sender, EventArgs e)
        {
            //filtro por código
            try
            {
                this.bindingSource1.Filter = "Material = '" + this.tstxtFiltro.Text + "'";
            }
            catch (Exception)
            {

                MessageBox.Show("Error al aplicar filtro!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
